package cn.zlj.nmcc.web.controller;

import cn.zlj.nmcc.domain.Tj;
import cn.zlj.nmcc.query.TjQuery;
import cn.zlj.nmcc.result.JSONResult;
import cn.zlj.nmcc.result.PageList;
import cn.zlj.nmcc.service.ITjService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tj")
public class TjController {

    @Autowired
    public ITjService tjService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Tj tj){
        if(tj.getId()!=null){
            tjService.updateById(tj);
        }else{
            tjService.insert(tj);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        tjService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        Wrapper<Tj> wrapper=new EntityWrapper<>();
        wrapper.eq("user_id",id);
        Tj tj = tjService.selectOne(wrapper);
        return JSONResult.success(tj);
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(tjService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody TjQuery query){
        Page<Tj> page = new Page<Tj>(query.getPage(),query.getRows());
        page = tjService.selectPage(page);
        return JSONResult.success(new PageList<Tj>(page.getTotal(),page.getRecords()));
    }
}
